<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul {
            padding: 0;
            margin: 0;
            list-style: none;
            width: 1000px;
            margin: auto;
            display: flex;
            flex-wrap: wrap;
        }

        li {
            width: 23%;
            margin: 1%;
            border: 1px solid #000;
            box-sizing: border-box;
            text-align: center;
        }

        img {
            width: 100%;
            height: 200px;
        }
    </style>
</head>

<body>

    <header>
        用户名: <strong>xx</strong>
        <span>退出账号</span>
    </header>


    <div class="search">
        <input type="text">
        <button>搜索</button>
    </div>


    <div>
        按照价格排序
        <button class="asc">升序</button>
        <button class="desc">降序</button>
    </div>

    <ul>
        <!-- <li>
            <a href="">
                <img src="" alt="">
                <h3></h3>
                <p></p>
                <strong></strong>
            </a>
        </li> -->
    </ul>



</body>

</html>


<script src="../js/tools.js"></script>

<script>

    // 关键字
    let kw = '' ;
    // 搜索
    const okw = document.querySelector('.search input');
    const oBtn = document.querySelector('.search button');
    // 列表
    const oUl = document.querySelector('ul');

    const oAsc = document.querySelector('.asc');
    const oDesc = document.querySelector('.desc');




    getData() ;



    oBtn.addEventListener('click', function () {
        // 关键字
        kw = okw.value;
        getData()
    })


    oAsc.addEventListener('click' , function() {
        ajax({
            type: 'get',
            path: '../php/sort_list.php',
            data : {
                sort : 'asc'
            },
            success: res => {
                console.log(res);
                const { status, msg, list } = res;
                if (status) {
                    let html = '';
                    list.forEach(v => {
                        const {
                            goods_img,
                            goods_title,
                            goods_des,
                            goods_price
                        } = v;
                        html += `
                        <li>
                            <a href="">
                                <img src="${goods_img}" alt="">
                                <h3>${goods_title}</h3>
                                <p>${goods_des}</p>
                                <strong>${goods_price}</strong>
                            </a>
                        </li>
                        `
                        const oUl = document.querySelector('ul');
                        oUl.innerHTML = html;
                    })
                } else {
                    alert(msg)
                }
            }
        })
    })

    oDesc.addEventListener('click' , function() {
        ajax({
            type: 'get',
            path: '../php/sort_list.php',
            data : {
                sort : 'desc'
            },
            success: res => {
                console.log(res);
                const { status, msg, list } = res;
                if (status) {
                    let html = '';
                    list.forEach(v => {
                        const {
                            goods_img,
                            goods_title,
                            goods_des,
                            goods_price
                        } = v;
                        html += `
                        <li>
                            <a href="">
                                <img src="${goods_img}" alt="">
                                <h3>${goods_title}</h3>
                                <p>${goods_des}</p>
                                <strong>${goods_price}</strong>
                            </a>
                        </li>
                        `
                        const oUl = document.querySelector('ul');
                        oUl.innerHTML = html;
                    })
                } else {
                    alert(msg)
                }
            }
        })
    })


    
    function getData() {
        ajax({
            type: 'get',
            path: '../php/list.php',
            data : {
                kw
            },
            success: res => {
                console.log(res);
                const { status, msg, list } = res;
                if (status) {
                    let html = '';
                    list.forEach(v => {
                        const {
                            goods_img,
                            goods_title,
                            goods_des,
                            goods_price,
                            goods_id
                        } = v;
                        html += `
                        <li>
                            <a href="./05detail.html?id=${goods_id}">
                                <img src="${goods_img}" alt="">
                                <h3>${goods_title}</h3>
                                <p>${goods_des}</p>
                                <strong>${goods_price}</strong>
                            </a>
                        </li>
                        `
                        const oUl = document.querySelector('ul');
                        oUl.innerHTML = html;
                    })
                } else {
                    alert(msg)
                }
            }
        })

    }


</script>